<template>
  <div class="app-container">
    <div
      class="content-container"
      v-if="
        !constructionLinkFormVisible &&
        !constructionSchemeForVisible &&
        !constructionMaterialInstallFormVisible
      "
    >
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="姓名">
            <el-input v-model="queryParams.ownName" placeholder="请输入姓名" clearable />
          </el-form-item>
          <!-- <el-form-item prop="keywords" label="联系方式">
            <el-input v-model="queryParams.mobile" placeholder="请输入联系方式" clearable />
          </el-form-item> -->

          <el-form-item>
            <el-button type="primary" icon="search" @click="handleSearch">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            @handleQuery="changePage"
          >
            <template #type="{ row, name }">
              {{ getLabelByValue(CONTACT_TYPE, row.type) }}
            </template>
            <template #constructionLinkStatus="{ row, name }">
              <el-tag :type="HOUSE_STATUS_MAP[row.house.constructionLinkStatus] || 'info'">
                {{ getLabelByValue(HOUSE_STATUS, row.house.constructionLinkStatus) }}
              </el-tag>
            </template>
            <template #constructionSchemeStatus="{ row, name }">
              <el-tag :type="HOUSE_STATUS_MAP[row.house.constructionSchemeStatus] || 'info'">
                {{ getLabelByValue(HOUSE_STATUS, row.house.constructionSchemeStatus) }}
              </el-tag>
            </template>
            <template #constructionMaterialInstallStatus="{ row, name }">
              <el-tag
                :type="HOUSE_STATUS_MAP[row.house.constructionMaterialInstallStatus] || 'info'"
              >
                {{ getLabelByValue(HOUSE_STATUS, row.house.constructionMaterialInstallStatus) }}
              </el-tag>
            </template>
            <template #endStatus="{ row }">
              <el-tag :type="HOUSE_STATUS_MAP[row.house.endStatus]">
                {{ getLabelByValue(HOUSE_STATUS, row.house.endStatus) }}
              </el-tag>
            </template>
            <template #operation="{ row, name }">
              <el-button link type="primary" icon="Edit" @click="handleEditConstructionLink(row)">
                施工关联
              </el-button>
              <el-button link type="primary" icon="Edit" @click="handleEditConstructionScheme(row)">
                组织施工
              </el-button>
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleEditConstructionMaterialInstall(row)"
              >
                材料进场
              </el-button>
              <el-button link type="primary" icon="Finished" @click="handleEditisFinal(row)">
                乔迁之喜
                <!-- :disabled="isEnd(row.house)" -->
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <construction-scheme-form
      v-if="constructionSchemeForVisible"
      @back-to="constructionSchemeForVisible = false"
      ref="constructionSchemeFormRef"
      @load-data="handleQuery"
    ></construction-scheme-form>
    <construction-link-form
      v-if="constructionLinkFormVisible"
      ref="constructionLinkFormRef"
      @back-to="constructionLinkFormVisible = false"
      @load-data="handleQuery"
      :merchantUserId="merchantUserId"
    ></construction-link-form>
    <construction-material-install-form
      v-if="constructionMaterialInstallFormVisible"
      ref="constructionMaterialInstallFormRef"
      @back-to="constructionMaterialInstallFormVisible = false"
      @load-data="handleQuery"
    ></construction-material-install-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import HouseAPI, { HousePageQuery, HousePageVO } from "@/api/house/index";
import { getLabelByValue } from "@/utils/enums";
import { CONTACT_TYPE, UserTypeEnum } from "../../../enums/system/UserEnum";
import { BIND_STATUS_ENUM } from "@/enums/contact/ContactEnum";
import { HOUSE_STATUS, HOUSE_STATUS_MAP } from "@/enums/house/house-const";
import ConstructionSchemeForm from "@/views/house/houseConstruction/components/construction-scheme-form.vue";
import ConstructionLinkForm from "@/views/house/houseConstruction/components/construction-link-form.vue";
import ConstructionMaterialInstallForm from "@/views/house/houseConstruction/components/construction-material-install-form.vue";

const loading = ref(false);
const constructionSchemeFormRef = ref();
const constructionSchemeForVisible = ref(false);
const constructionLinkFormRef = ref();
const constructionLinkFormVisible = ref(false);
const constructionMaterialInstallFormRef = ref();
const constructionMaterialInstallFormVisible = ref(false);
const total = ref(0);
const queryFormRef = ref();
const merchantUserId = ref();

const queryParams = reactive<HousePageQuery>({
  ownName: "",
  mobile: "",
  stepInfoList: [
    "STEP_CONSTRUCTION_LINK",
    "STEP_CONSTRUCTION_SCHEME",
    "STEP_CONSTRUCTION_MATERIAL_INSTALL",
  ],
  typeList: [CONTACT_TYPE.PROJECT_MANAGER.value],
  status: BIND_STATUS_ENUM.SUCCESS.value,
  userType: UserTypeEnum.DESIGNER.value,
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref<HousePageVO[]>([]);
const tableColumnsData = ref([
  {
    prop: "house.ownName",
    label: "户主名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.region",
    label: "小区地址",
    showOverflowTooltip: true,
  },
  {
    prop: "house.neighborhood",
    label: "小区名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.houseNumber",
    label: "门牌号",
    showOverflowTooltip: true,
    width: 150,
  },
  {
    prop: "house.size",
    label: "面积（平方米）",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    prop: "type",
    label: "通讯录类型",
    width: 120,
    showOverflowTooltip: true,
    customSlot: "type",
  },
  {
    label: "施工关联状态",
    width: 120,
    showOverflowTooltip: true,
    customSlot: "constructionLinkStatus",
  },
  {
    label: "组织施工状态",
    width: 120,
    showOverflowTooltip: true,
    customSlot: "constructionSchemeStatus",
  },
  {
    label: "材料进场状态",
    width: 120,
    showOverflowTooltip: true,
    customSlot: "constructionMaterialInstallStatus",
  },
  {
    label: "乔迁之喜状态",
    width: 120,
    showOverflowTooltip: true,
    customSlot: "endStatus",
  },
  {
    label: "乔迁之喜审核",
    width: 120,
    showOverflowTooltip: true,
    prop: "house.auditDesc",
  },
  {
    prop: "operation",
    label: "操作",
    width: 400,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);
function handleSearch() {
  queryParams.pageNum = 1;
  handleQuery();
}

// 查询
function handleQuery() {
  loading.value = true;
  HouseAPI.getHouseContactPage(queryParams)
    .then((data) => {
      tableData.value = data.list || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.mobile = "";
  queryParams.ownName = "";
  handleQuery();
}
function changePage(val: any) {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  handleQuery();
}

async function handleEditConstructionMaterialInstall(val) {
  let cannotEdit = val.house.constructionMaterialInstallStatus === HOUSE_STATUS.APPROVED.value;
  constructionMaterialInstallFormVisible.value = true;
  await nextTick();
  constructionMaterialInstallFormRef.value.showModal(val, cannotEdit);
}
async function handleEditConstructionLink(val) {
  let cannotEdit = val.house.constructionLinkStatus === HOUSE_STATUS.APPROVED.value;
  merchantUserId.value = val.merchantUserId;
  constructionLinkFormVisible.value = true;
  await nextTick();
  constructionLinkFormRef.value.showModal(val, cannotEdit);
}
async function handleEditConstructionScheme(val) {
  let cannotEdit = val.house.constructionSchemeStatus === HOUSE_STATUS.APPROVED.value;
  constructionSchemeForVisible.value = true;
  await nextTick();
  constructionSchemeFormRef.value.showModal(val, cannotEdit);
}
async function handleEditisFinal(val) {
  ElMessageBox.confirm("确定要已经完结全部装修流程吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await HouseAPI.triggerEndStatus({ houseId: val.houseId });
    ElMessage.success("成功");
    handleQuery();
  });
}
function isEnd(val) {
  console.log(val, "val");
  // 如果有状态是0，就不让乔迁之喜填写
  const status = [
    "constructionLinkStatus",
    "constructionMaterialBuyStatus",
    "constructionMaterialInstallStatus",
    "constructionSchemeStatus",
    "designBudgetStatus",
    "designLinkStatus",
    "designSchemeStatus",
    "endStatus",
  ];
  return status.some((item) => val[item] === 0);
}

onMounted(async () => {
  handleQuery();
});
</script>
